<?php

namespace app\wxapi\controller;

use think\Controller;
use app\common\traits\controller\TraitController;
use Endroid\QrCode\QrCode;
use think\Request;
use think\Response;
use think\Session;
use think\Cookie;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
use Carbon\Carbon;
use think\helper\Str;

abstract class Api extends Controller
{
	use TraitController;

    protected $app = '';

    protected $open_id = '';

    protected $options = [
            /**
             * Debug 模式，bool 值：true/false
             *
             * 当值为 false 时，所有的日志都不会记录
             */
            'debug'  => false,
            /**
             * 账号基本信息，请从微信公众平台/开放平台获取
             */
            'app_id'  => 'wx5a1090e16a2f3e95',         // AppID
            'secret'  => '7d7f576e91c8e8dc1af4fbeb6938ac2a',     // AppSecret
            'token'   => 'i9ch3f2ca',          // Token
            'aes_key' => 'RBuTcKRnHXbrLr4mrjQJW16ezM0UlggeHJZfu09ecnf',                    // EncodingAESKey，安全模式下请一定要填写！！！
            /**
             * 日志配置
             *
             * level: 日志级别, 可选为：
             *         debug/info/notice/warning/error/critical/alert/emergency
             * file：日志文件位置(绝对路径!!!)，要求可写权限
             */
            // 'log' => [
            //     'level' => 'debug',
            //     'file'  => '/tmp/easywechat.log',
            // ],
            /**
             * OAuth 配置
             *
             * scopes：公众平台（snsapi_userinfo / snsapi_base），开放平台：snsapi_login
             * callback：OAuth授权完成后的回调页地址
             */
            'oauth' => [
                'scopes'   => ['snsapi_base'],
                'callback' => '/user/callback',
            ],
            /**
             * 微信支付
             */
            'payment' => [
                'merchant_id'        => '1419891902',
                'key'                => '845d5f1153c27beed29f479640445148',
                'cert_path'          => 'path/to/your/cert.pem', // XXX: 绝对路径！！！！
                'key_path'           => 'path/to/your/key',      // XXX: 绝对路径！！！！
                // 'device_info'     => '013467007045764',
                // 'sub_app_id'      => '',
                // 'sub_merchant_id' => '',
                // ...
            ],
            /**
             * Guzzle 全局设置
             *
             * 更多请参考： http://docs.guzzlephp.org/en/latest/request-options.html
             */
            'guzzle' => [
                'timeout' => 3.0, // 超时时间（秒）
                'verify' => false, // 关掉 SSL 认证（强烈不建议！！！）
            ],
        ];

    protected $beforeActionList = [
        'auth' =>  ['except'=>'getcallback, getlogin, getlogout']
    ];

	protected function init()
    {
        if ($this->request->get('test')) {
            Cookie::set('auth_id', $this->request->get('test'));
        }
        
        $this->app = new Application($this->options);
        $this->assign('auth_id', Cookie::get('auth_id'));
    }
    public function auth()
    {
        if (!Cookie::get('auth_id')) {
            //$request->url()
            $response = $this->app->oauth->scopes(['snsapi_base'])->redirect();
            $response->send();
        }else{
            $this->open_id = Cookie::get('auth_id');
        }
    }
}
